home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Source Code
/
Visual Basic Source Code.iso
/
vbsource
/
vbprnt20
/
vbprint.doc
< prev
next >
Wrap
Text File
|
1996-05-31
|
30KB
|
737 lines
VBPRINT Library v2.00 written by Robert Simpson (Black Castle Software, LLC)
This program has been released to the public domain. No warranties are
expressed or implied. Use at your own risk.
---Overview---
This DLL allows your Visual Basic applications to have better control over the
printer device. It seems to be the one area MS has consistently forgotten in
VB.
By far the easiest method for changing printer orientation, paper settings,
trays, color and etc is to use the DEVMODE structure and make calls to the
printer driver's ExtDeviceMode() function. Unfortunately this particular
function resides in the printer driver itself and VB cannot load that driver
and call pointers to functions within it. Therefore, I have written a set of
printer control functions which do exactly that. In addition, using the
VBResetDC() function, you can make changes to the printer orientation and
other settings during a print session (even if you're using VB's Printer
object).
One of the problems of course is that not all DEVMODE structures are created
equal. Depending on the printer, the driver may append its own private data
to the end of a DEVMODE structure, which further complicates the VB interface.
The trick here is that all calls to VBPRINT use a special DEVMODE structure
which I've modified for use with VB (hence called the VBDEVMODE). This
VBDEVMODE allows the VB Programmer access to all the important items in the
DEVMODE structure and still allows the printer to have its own private area.
---Features---
Ability to get/set the Windows default printer
Ability to list all the installed printers
Allows VB to call the ExtDeviceMode() function of a printer
Also allows VB to call the DeviceCapabilities() function of a printer
Has special VB version of ResetDC() to change printer settings on the fly
Uses a special DEVMODE structure specifically tailored for VB
Can convert a VBDEVMODE structure from and to a VB String so it can be easily
saved to disk and restored later
What this means is, you now have an easy method for changing print orientation
and any other printer settings through the use of the VBDEVMODE structure.
---Flavors of this Library (and why there ARE flavors!!)---
This library comes in 3 flavors so just about any VB programmer can use it
A VB3 version of the DLL (which is compatible with VB4/16-bit)
A VB4/16-bit version (which is NOT compatible with VB3!!)
A VB4/32-bit version (who's functions are identical to the VB4/16-bit version)
Why is the VB3 version compatible with VB4 and the VB4 version not compatible
with VB3? I decided that I wanted the VB4/32-bit and VB4/16-bit versions to
be functionally identical, which meant that I had to allow for the fact that
VB4/32-bit uses UNICODE strings (16-bits per character), and therefore I could
not always write directly into memory because VB4 often does some unwanted
translations when making function calls.
The particular function affected (and the ONLY one affected) is the
VBDeviceCapabilities() function. In the VB3 version, there is only ONE
command, VBDeviceCapabilities(). Often this function returns an array of data
to the user, which is no problem in VB3 or VB4/16-bit, you just pass the first
dimension of the array (such as Names$(0) ) and the DLL can write all the
elements of the array since they're stored linearly in memory.
NOT SO in VB4/32-bit!! If you pass Names$(0) to a DLL, VB4 translates it from
UNICODE to ANSI and gives you a new pointer to JUST THAT DIMENSION, leaving me
no place to put the rest of the items that are supposed to go in the array.
Therefore in order to make your VB code portable from 16-bits to 32-bits,
the VB4 versions of the DLL have a VBDeviceCapArray() function, which is
just an Alias to "VBDeviceCapabilities" but one that explicitly requires an
array variable, which you would pass as Names$() (with NO numbers between the
parens!).
So if you are porting a VB3 program to use the NEW VB4 VBPRNTxx library, you
will need to replace commands such as:
i = VBDeviceCapabilities(DefPrinter$,DC_PAPERNAMES,Names$(0),inDEV)
to the new format for arrays in the VB4 version:
i = VBDeviceCapArray(DefPrinter$,DC_PAPERNAMES,Names$(),inDEV)
**THIS IS THE ONLY DIFFERENCE IN FUNCTION CALLS**
---Using this DLL with the Printer Object---
Most printing in VB goes through the Printer object, which leaves much to be
desired. Normally in the Windows API you can pass a DEVMODE structure when
you create a device context, which makes life much easier--you can include all
your custom printer settings in the device open sequence and not have to
bother with a bunch of subsequent Escape() functions.
The VB Printer object doesn't allow this convenience. Therefore if you must
use the VB Printer object, you have two options:
(Assuming you've already got a VBDEVMODE structure filled in with your desired
settings)
#1
Use the Printer object's .hDC property and call VBResetDC() with the filled-
in VBDEVMODE structure. This is the easiest method...or...
#2
Call VBExtDeviceMode() and get the printer's current settings (see the
functions reference for instructions on doing this)
Make another call to VBExtDeviceMode() with your DEVMODE structure and tell
the printer to use these settings as the new default settings.
Use the Printer object to your heart's content
When finished, one more call to VBExtDeviceMode() with the old default
settings, restoring them back to the original.
Using the utilities in this DLL, you'll be able to change printers, use them
in ways you were unable to do previously, and change back to the default
printer all without an annoying dialog box or having to prompt the user.
---The DEVMODE structure---
If you're familiar with the DEVMODE structure, this one should look strikingly
similar.
Type DEVMODE_TYPE
dmDeviceName As String * 32 ' VB4/32 uses an array of Byte
dmSpecVersion As Integer
dmDriverVersion As Integer
dmSize As Integer
dmDriverExtra As Integer
dmFields As Long
dmOrientation As Integer
dmPaperSize As Integer
dmPaperLength As Integer
dmPaperWidth As Integer
dmScale As Integer
dmCopies As Integer
dmDefaultSource As Integer
dmPrintQuality As Integer
dmColor As Integer
dmDuplex As Integer
dmYResolution As Integer
dmTTOption As Integer
dmPrivate As String
End Type
The 32-bit version adds several items to the above structure, and uses arrays
of Byte instead of fixed-length strings because of the fact that strings are
stored internally in UNICODE format (which means they're twice as large)
The following is an exerpt from the Win3.1 SDK describing the elements of the
DEVMODE structure:
dmDeviceName
Specifies the name of the device the driver supports--for example, "PCL/HP
LaserJet" in the case of the Hewlett-Packard LaserJet. Each driver has a
unique string.
dmSpecVersion
Specifies the version number of the DEVMODE structure. For Windows version
3.1, this value should be 0x30A.
dmDriverVersion
Specifies the printer driver version number assigned by the printer driver
developer.
dmSize
Specifies the size, in bytes, of the DEVMODE structure. (This value does not
include the optional dmDriverData member for device-specific data, which can
follow the structure.) If an application manipulates only the driver-
independent portion of the data, it can use this member to find out the length
of the structure without having to account for different versions.
dmDriverExtra
Specifies the size, in bytes, of the optional dmDriverData member for device-
specific data, which can follow the structure. If an application does not use
device-specific information, it should set this member to zero.
dmFields
Specifies a set of flags that indicate which of the remaining members in the
DEVMODE structure have been initialized. It can be any combination (or it can
be none) of the following values:
Constant Value
DM_ORIENTATION 0x0000001L
DM_PAPERSIZE 0x0000002L
DM_PAPERLENGTH 0x0000004L
DM_PAPERWIDTH 0x0000008L
DM_SCALE